package org.example.short_link.admin.remote;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.example.short_link.admin.remote.dto.req.*;
import org.example.short_link.admin.remote.dto.resp.ShortLinkCreateRespDTO;
import org.example.short_link.admin.remote.dto.resp.ShortLinkGroupCountQueryRespDTO;
import org.example.short_link.admin.remote.dto.resp.ShortLinkPageRespDTO;
import org.example.short_link.admin.remote.dto.resp.ShortLinkStatsRespDTO;
import org.example.short_link.project.dto.req.RecycleBinRecoverDTO;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.example.short_link.admin.common.convention.result.Result;
import java.util.HashMap;
import java.util.List;

/**
 * 短链接中台远程调用层
 *
 */
public interface ShortLinkRemoteService {






    /**
     *
     * 后管调用中台分页查询
     * @param requestParam
     * @return
     */
    default Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO requestParam){
        HashMap<String, Object> requestMap = new HashMap<>();
        requestMap.put("gid",requestParam.getGid());
        requestMap.put("current",requestParam.getCurrent());
        requestMap.put("size",requestParam.getSize());
        String resultPage = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/group/page", requestMap);

        return JSON.parseObject(resultPage, new TypeReference<Result<IPage<ShortLinkPageRespDTO>>>() {

        });
    }

    /**
     * 通过后管调用中台创建短链接
     * @param requestParam
     * @return
     */
    default Result<ShortLinkCreateRespDTO> createShortLink(ShortLinkCreateReqDTO requestParam){

        /**
         * TypeReference是 Java 程序语言中的一个泛型类，它通常用于获取某个泛型的类型信息。
         * 在一些情况下，特别是涉及到反序列化（将 JSON 字符串或其他数据格式转换为 Java 对象）时，由于 Java 的类型擦除机制，
         * 在运行时可能无法直接获取到泛型的具体类型信息。而通过使用TypeReference，可以明确指定反序列化的目标类型。
         */
        String resultBody = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/group/create", JSON.toJSONString(requestParam));
        return JSON.parseObject(resultBody, new TypeReference<>() {});

    }

    /**
     * 查询短链接数量
     * @param requestParam 查询短链接数量请求参数
     * @return 查询短链接数量请求响应
     */
    default Result<List<ShortLinkGroupCountQueryRespDTO>> ShortLinkCount(List<String> requestParam){
        HashMap<String, Object> requestMap = new HashMap<>();
        requestMap.put("requestParam",requestParam);
        String resuleMap = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/group/count", requestMap);
        return JSON.parseObject(resuleMap, new TypeReference<>() {
        });
    }

    /**
     * 通过后管调用中台修改短链接
     * @param requestParam 修改短链接请求参数
     * @return
     */
    default void updateShortLink(ShortLinkUpdateReqDTO requestParam){
       HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/group/update", JSON.toJSONString(requestParam));
    }


    /**
     * 根据 URL 获取标题
     * @param url 目标网站地址
     * @return 网站标题
     */
    default Result<String> getTitleByUrl(@RequestParam("url") String url){
        String urlName = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/title?url=" + url);
        return JSON.parseObject(urlName, new TypeReference<>() {
        });
    }

    /**
     * 回收站回收功能
     * @param requesParam 回收请求参数
     */
    default void RecycleBinSave(@RequestBody RecycleBinSaveDTO requesParam){
        HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/save", JSON.toJSONString(requesParam));

    }

    /**
     * 回收站分页功能
     * @param requestParam
     * @return
     */
    default Result<IPage<ShortLinkPageRespDTO>> pageRecycle(RecyclePageReqDTO requestParam) {
        HashMap<String, Object> requestMap = new HashMap<>();
        requestMap.put("gidList",requestParam.getGidList());
        requestMap.put("current",requestParam.getCurrent());
        requestMap.put("size",requestParam.getSize());
        String resultPage = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/page", requestMap);

        return JSON.parseObject(resultPage, new TypeReference<Result<IPage<ShortLinkPageRespDTO>>>() {

        });
    }

    /**
     * 回收站恢复接口调用
     * @param requestParam
     * @return
     */
    default void RecycleBinRecover(@RequestBody RecycleBinRecoverDTO requestParam){
        HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/recover", JSON.toJSONString(requestParam));
    }

    /**
     * 回收站移除接口
     * @param requestParam
     * @return
     */
    default void RecycleBinRemove(@RequestBody RecycleBinRemoveDTO requestParam){
        HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/remove", JSON.toJSONString(requestParam));
    }


    /**
     * 统计一天的短链接监控记录
     * @param requestParam
     * @return
     */
    default ShortLinkStatsRespDTO oneShortLinkStats(ShortLinkStatsReqDTO requestParam){
        HashMap<String, Object> requestMap = new HashMap<>();
        requestMap.put("fullShortUrl",requestParam.getFullShortUrl());
        requestMap.put("gid",requestParam.getGid());
        requestMap.put("startDate",requestParam.getStartDate());
        requestMap.put("endDate",requestParam.getEndDate());
        requestMap.put("enableStatus", requestParam.getEnableStatus());


        String resultStats = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/stats", requestMap);
        return JSON.parseObject(resultStats, new TypeReference<>() {});

    }

}
